// pages/app-ywb-home/app-ywb-home.js

//获取应用实例
const app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    client: [],
    project: [],
    contract: [],
    isXzjl: 0,
    role: "", 
    username: "",
    approvelCount: 0,
    isDeptOrGroup: 0,
    isXmry: 0,
	textkhmc: '',
	khmcId:'',

  },
  // 客户名称模糊搜索
  changeSearch: function (event) {
    console.log(event.detail);
    let that = this;
    var inputSearch = event.detail.value;
    wx.request({
      url: app.globalData.apiUrl + '/wx/AllClientData', //接口地址
      data: {
		loginName: app.globalData.loginName,
        clientName: inputSearch,
      },
      success: function (res) {
        console.log("数据字典=", res)
		if(res.data.list.length==0){
			that.setData({
			  textkhmc: '',
			  khmcId: '',
			})
		}
        else{
			that.setData({
			  textkhmc: res.data.list[0].clientname,
			  khmcId: res.data.list[0].id,
			})
		}
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    this.getHomePageLogin();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getHomePage();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  clearLoginName: function(){
    console.log("推出登录，清除缓存 。。")
    wx.setStorageSync('loginName', null);
    app.globalData.loginName = null;
  },

  getHomePage: function () {
    var that = this;       
    if (app.globalData.loginName == null) {
      // wx.showToast({
      //   title: '请输入账号：',
      //   icon: 'none',
      //   duration: 2000
      // })
      wx.reLaunch({
        //url: '/pages/login/login'	//无loginName跳转到登录页
      })
      return;
    }
        wx.request({
          url: app.globalData.apiUrl + '/wx/homepage', //接口地址
          data: {
            loginName: app.globalData.loginName,

          },
          success: function (res) {
            console.log("首页信息=", res)
            // wx.setStorage({   //存储数据并准备发送给下一页使用
            //   key: "phoneObj",
            //   data: res.data.phoneNumber,
            // })
            that.setData({
              client:res.data.data.client.list,
              project: res.data.data.project.list,
              contract: res.data.data.contract.list,
              isXzjl: res.data.data.isXzjl,
              role: res.data.data.role,
              username: res.data.data.username,
              approvelCount: res.data.data.approvelCount,
              isDeptOrGroup: res.data.data.isDeptOrGroup,
              isXmry: res.data.data.isXmry,
            })
          }
        })
  },

  getHomePageLogin: function () {
    var that = this;
    if (app.globalData.loginName == null) {
      // wx.showToast({
      //   title: '请输入账号：',
      //   icon: 'none',
      //   duration: 2000
      // })
      wx.reLaunch({
        url: '/pages/login/login'	//无loginName跳转到登录页
      })
      return;
    }
    wx.request({
      url: app.globalData.apiUrl + '/wx/homepageLogin', //接口地址
      data: {
        loginName: app.globalData.loginName,

      },
      success: function (res) {
        console.log("首页登录信息=", res)
        // wx.setStorage({   //存储数据并准备发送给下一页使用
        //   key: "phoneObj",
        //   data: res.data.phoneNumber,
        // })
        that.setData({
          client: res.data.data.client.list,
          project: res.data.data.project.list,
          contract: res.data.data.contract.list,
          isXzjl: res.data.data.isXzjl,
          role: res.data.data.role,
          username: res.data.data.username,
          approvelCount: res.data.data.approvelCount,
          isDeptOrGroup: res.data.data.isDeptOrGroup,
          isXmry: res.data.data.isXmry,
        })
      }
    })
  },

})